package com.ytkj.digitalConstruction.service.assets;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ytkj.digitalConstruction.dao.assets.DeviceMapper;
import com.ytkj.digitalConstruction.entity.assets.Device;
import com.ytkj.digitalConstruction.enums.asset.AssetStatus;
import com.ytkj.digitalConstruction.httpbean.PageParam;
import com.ytkj.digitalConstruction.httpbean.assets.resbean.ResDevice;
import com.ytkj.digitalConstruction.httpbean.project.ResProject;
import com.ytkj.digitalConstruction.service.project.ProjectService;

/**
 * Device表业务层实现
 * 
 * @author
 * @date 2017年12月1日 下午4:16:38
 * @return
 */
@Service
public class DeviceService {

	@Autowired
	private DeviceMapper deviceMapper;

	@Autowired
	private ProjectService projectService;

	/**
	 * 根据id查询设备
	 * 
	 * @param deviceId
	 * @return
	 */
	public Device selectByDeviceIdPrimaryKey(Integer deviceId) {
		return deviceMapper.selectByDeviceIdPrimaryKey(deviceId);
	}

	/**
	 * 增加设备操作
	 * 
	 * @param devicer
	 * @param request
	 * @return
	 */
	public Integer insert(Device device) {
		// 添加设备默认可用
		device.setStatus(IAssets.USABLE);
		return deviceMapper.insertSelective(device);
	}

	/**
	 * 修改设备操作
	 * 
	 * @param device
	 * @param request
	 * @return
	 */
	public boolean update(Device device) {
		return deviceMapper.updateByPrimaryKeySelective(device) > 0;
	}

	/**
	 * 设备是否存在
	 * 
	 * @param device
	 * @return
	 */
	public boolean deviceExists(Device device) {
		return deviceMapper.selectByPrimaryKey(device.getDeviceId()) != null;
	}

	/**
	 * 删除设备操作
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	public Integer delete(Integer id) {
		return deviceMapper.updateByIdPrimaryKey(id);
	}

	/**
	 * 分页查找所有设备
	 * 
	 * @param userId
	 * @param pageParam
	 * @return
	 */
	public Page<ResDevice> select(int userId, PageParam pageParam) {
		List<Integer> projectIds = new ArrayList<>();
		List<ResProject> projects = projectService.getProjects(userId);
		for (ResProject project : projects) {
			int projectId = project.getProjectId();
			if (projectIds.contains(projectId)) {
				continue;
			}
			projectIds.add(projectId);
		}
		if (projectIds.size() == 0) {
			return new Page<>();
		}
		if (pageParam != null) {
			PageHelper.startPage(pageParam.getPageCurrent(), pageParam.getPageSize());
		}
		Page<ResDevice> page = deviceMapper.selectAllPageable(projectIds);
		for (ResDevice device : page) {
			device.setStatusName(AssetStatus.ofValue(device.getStatus()).string());
		}
		return page;
	}

	/**
	 * 查询所有设备
	 * 
	 * @param userId
	 * @return
	 */
	public List<ResDevice> selectAll(int userId) {
		return select(userId, null);
	}

	/**
	 * 根据用户id查找绑定设备
	 * 
	 * @param userId
	 * @return
	 */
	public List<Device> findBindDevices(int userId) {
		List<ResProject> projects = projectService.getProjects(userId);
		List<Device> devices = new ArrayList<>();
		for (int i = 0; i < projects.size(); i++) {
			int projectId = projects.get(i).getProjectId();
			List<Device> ds = deviceMapper.selectBindDeviceByprojectId(projectId);
			devices.addAll(ds);
		}
		return devices;
	}

}
